﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DONGACS.Domain;
using QLPKWeb.BussinessDelegate.Base;
using QLPKWeb.Models.FormBean.PhanQuyen;
using DONGACS.Common;
using DONGACS.DTO;
using DONGACS.SERVICE;
using Webdiyer.WebControls.Mvc;

namespace QLPKWeb.BussinessDelegate.PhanQuyen
{
    public class JobTitlesServiceDelegate : BaseServiceDelegate<JobTitlesDto, JobTitles>
    {
        JobTitlesService JobTitlesService = new JobTitlesService();

        //public IList<JobTitlesDto> GetAllJobTitles()
        //{
        //    return JobTitlesService.GetAllJobTitles();
        //}

        public JobTitlesDto GetJobTitlesById(string id)
        {
            return JobTitlesService.GetJobTitlesById(id);
        }

        public JobTitlesDto BuildNewJobTitles(JobTitlesForm form)
        {
            JobTitlesDto newJobTitle = new JobTitlesDto();
            newJobTitle.JobTitle = form.JobTitle;
            newJobTitle.Description = form.Description;
            newJobTitle.CreateDate = DateTime.Now;
            newJobTitle.UpdateDate = DateTime.Now;
            newJobTitle.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newJobTitle.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newJobTitle.IsDelete = false;

            return newJobTitle;
        }

        public JobTitlesDto BuildEditJobTitles(JobTitlesForm form)
        {
            JobTitlesDto newJobTitle = this.JobTitlesService.GetJobTitlesById(form.ID.ToString());
            newJobTitle.JobTitle = form.JobTitle;
            newJobTitle.Description = form.Description;
            //newJobTitle.CreateDate = DateTime.Now;
            newJobTitle.UpdateDate = DateTime.Now;
            //newJobTitle.CreateBy = SessionDetail.USERID;
            newJobTitle.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            //newJobTitle.IsDelete = false;

            return newJobTitle;
        }

        public bool AddNewJobTitles(JobTitlesDto jobTitlesDto)
        {
            return this.JobTitlesService.AddNewJobTitles(jobTitlesDto);
        }

        public bool AddNewJobTitles(JobTitlesDto jobTitlesDto, ref int idReturn)
        {
            return this.JobTitlesService.AddNewJobTitles(jobTitlesDto, ref idReturn);
        }

        public bool Update(JobTitlesDto jobTitlesDto)
        {
            return this.JobTitlesService.Update(jobTitlesDto);
        }

        public bool ShiftDelete(string jobTitlesId)
        {
            return this.JobTitlesService.ShiftDelete(jobTitlesId);
        }

        public bool Delete(string jobTitlesId)
        {
            return this.JobTitlesService.Delete(jobTitlesId);
        }

        public JobTitlesDto GetJobTitleByName(string jobTitleName)
        {
            return this.JobTitlesService.GetJobTitleByName(jobTitleName);
        }

        public SelectList BuildJobTitleSelectList()
        {
            IList<JobTitlesDto> list;
            list = JobTitlesService.GetAllEntities(); //test

            IList<object> selectList = new List<object> { new { value = 0, Text = "[-- Choose Job Title --]" } };

            foreach (var dto in list)
            {
                selectList.Add(new { value = dto.ID, Text = dto.JobTitle });
            }
            return new SelectList(selectList, "Value", "Text");
        }

        //search
        public JobTitlesDto BuildJobTitleDTo(QLPKWeb.Models.LinQ.Common.TB_JobTitle JobTitle)
        {
            JobTitlesDto obj = new JobTitlesDto();
            obj.ID = JobTitle.ID;
            obj.JobTitle = JobTitle.JobTitle;
            obj.Description = JobTitle.Description;
            obj.CreateDate = (JobTitle.CreateDate != null ? JobTitle.CreateDate.Value : DateTime.MinValue);
            obj.UpdateDate = (JobTitle.UpdateDate != null ? JobTitle.UpdateDate.Value : DateTime.MinValue);
            obj.CreateBy = (JobTitle.CreateBy != null ? JobTitle.CreateBy.Value : 0);
            obj.UpdateBy = (JobTitle.UpdateBy != null ? JobTitle.UpdateBy.Value : 0);
            obj.IsDelete = (JobTitle.IsDelete != null ? JobTitle.IsDelete.Value : false);
            return obj;
        }

        public IList<JobTitlesDto> GetAllJobTitlesByName(string JobTitlesSearch)
        {
            QLPKWeb.Models.LinQ.Common.DBCommonDataContext db = new Models.LinQ.Common.DBCommonDataContext();
            List<QLPKWeb.Models.LinQ.Common.TB_JobTitle> ListJob =
                db.TB_JobTitles.Where(p => p.JobTitle.Contains(JobTitlesSearch.ToString()) && p.IsDelete == false).ToList();//ToPagedList(id ?? 1, DONGACS.Common.CommonVariables.PaggedItemsPerPage);
            IList<JobTitlesDto> List = new List<JobTitlesDto>();
            foreach (QLPKWeb.Models.LinQ.Common.TB_JobTitle item in ListJob)
            {
                JobTitlesDto obj = BuildJobTitleDTo(item);
                List.Add(obj);
            }
            return List;
            //PagedList<JobTitlesDto> pagelistJob=new PagedList<JobTitlesDto>(
        }
    }
}
